﻿/*
*************************************************************************

 Le logiciel de gestion d'EPN

 @project OpenEPN.
 @version 0.1
 @legals  (c) 2008 Damien Maillard, Sébastien Reinders. 
 Needed Components : GTK-Sharp 2.8. 
 Licensed under GPL v2.

 @author  Damien Maillard <damien.maillard@openepn.be>
 @author  Sébastien Reinders <sebastien.reinders@openepn.be>
 @author  Didier Piette <didier.piette@openepn.be>
 @author  Nicolas Karras <nicolas.karras@openepn.be>
 @author  Julien Poulin <julien.poulin@openepn.be>
 @since   2008.02.08
 @latest  

 This program is free software; you can 
 redistribute it and/or modify it under
 the terms of the GNU General Public License as 
 published by the Free Software
 Foundation; either version 2 of the License, or 
 (at your option) any later version.

 This program is distributed in the hope that it 
 will be useful, but WITHOUT ANY WARRANTY; without even 
 the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU General 
 Public License for more details.

 You should have received a copy of the GNU General 
 Public License along with this program; if not, 
 write to the Free Software Foundation, Inc., 59 Temple
 Place, Suite 330, Boston, MA 02111-1307 USA.
 
 * 
 * Conventions de nommage pour les variables locales :
 * m_ : pour les variables membres d'une méthode;
 * g_ : pour les variables membres d'une classe ;
 * c_ : pour les constantes;
 * s_ : pour les variables statiques;
 * e_ : pour les énumérations;
 * a_ : pour les Array et ArrayList;
 *  _ : pour les paramètres passés aux méthodes
 * T_ : T majuscule pour les Threads
 * E_ : E majuscule pour les événements
 * 
 
*************************************************************************
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using OeExceptions;

namespace DA.StreamXml
{

    /// <summary>
    /// Classe qui permet de lire le fichier xml des options du programme client
    /// </summary>
    public class OptionsConfigXml
    {
        
        #region "Déclarations"

        //ATTENTION - pour pouvoir retirer le System.Windows.Forms dans les using
        private string m_my_xml_file = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\xml\\OptionsConfig.xml";
        //private string m_my_xml_file = Application.StartupPath + ".\\xml\\OptionsConfig.xml";

        private XmlDocument m_xml_doc = new XmlDocument();
        private StreamReader m_strm;
        private string _glo_Error;

        //Noeud StreamFile
        private string m_xml_StreamFile_FileName;
        private string m_xml_StreamFile_Path;

        //Noeud AdminLocal
        private string m_xml_AdminLocal_Name;
        private string m_xml_AdminLocal_Login;
        private string m_xml_AdminLocal_Password;

        #endregion

        #region "Propriétés

        /// <summary>
        /// 
        /// </summary>
        public string File_name
        {
            get { return this.m_xml_StreamFile_FileName; }
            set { m_xml_StreamFile_FileName = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Path
        {
            get { return this.m_xml_StreamFile_Path; }
            set { this.m_xml_StreamFile_Path = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string AdminName
        {
            get { return this.m_xml_AdminLocal_Name; }
            set { this.m_xml_AdminLocal_Name = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string AdminLogin
        {
            get { return this.m_xml_AdminLocal_Login; }
            set { this.m_xml_AdminLocal_Login = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string AdminPassword
        {
            get { return this.m_xml_AdminLocal_Password; }
            set { this.m_xml_AdminLocal_Password = value; }
        }

        #endregion

        #region "Constructeur"

        /// <summary>
        /// Constructeur par défaut avec lecture du fichier xml
        /// </summary>
        public OptionsConfigXml()
        {
            try
            {
                this.m_strm = File.OpenText(this.m_my_xml_file);
            }
            catch (Exception e)
            {
                _glo_Error = ("Error while loading settings" + e.Message);
            }
            try
            {
                m_xml_doc.Load(m_strm);
            }
            catch (Exception e)
            {
                _glo_Error = ("Error while loading settings" + e.Message);
            }


            this.m_xml_StreamFile_FileName = m_xml_doc.SelectNodes("/Settings/StreamFile/FileName").Item(0).InnerText.Trim();
            this.m_xml_StreamFile_Path = m_xml_doc.SelectNodes("/Settings/StreamFile/Path").Item(0).InnerText.Trim();
            this.m_xml_AdminLocal_Name = m_xml_doc.SelectNodes("/Settings/AdminLocal/Name").Item(0).InnerText.Trim();
            this.m_xml_AdminLocal_Login = m_xml_doc.SelectNodes("/Settings/AdminLocal/Login").Item(0).InnerText.Trim();
            this.m_xml_AdminLocal_Password = m_xml_doc.SelectNodes("/Settings/AdminLocal/Password").Item(0).InnerText.Trim();

            m_strm.Close();
        }

        #endregion

        #region "implémentations"

        #region "Méthodes privées"

        //Pas de métjodes privées

        #endregion

        #region "Methodes publiques"

        /// <summary>
        /// Enregistrement des données dans le fichier xml
        /// </summary>
        public void SaveSettings()
        {
            try
            {

                m_xml_doc.SelectNodes("/Settings/StreamFile/FileName").Item(0).InnerText = this.m_xml_StreamFile_FileName;
                m_xml_doc.SelectNodes("/Settings/StreamFile/Path").Item(0).InnerText = this.m_xml_StreamFile_Path;
                m_xml_doc.SelectNodes("/Settings/AdminLocal/Name").Item(0).InnerText = this.m_xml_AdminLocal_Name;
                m_xml_doc.SelectNodes("/Settings/AdminLocal/Login").Item(0).InnerText = this.m_xml_AdminLocal_Login;
                m_xml_doc.SelectNodes("/Settings/AdminLocal/Password").Item(0).InnerText = this.m_xml_AdminLocal_Password;
                m_xml_doc.Save(m_my_xml_file);
            }
            catch (Exception ex)
            {
                XmlFileNotFoundException xmlfilenotfound = new XmlFileNotFoundException("Impossible d'écrire dans le fichier xml");
            }
        }

        #endregion

        #endregion
    }
}
